<?php
    class sscls_ProductModel extends sscls_Model
    {   

        function __construct()
        {
            parent::__construct();
        }
       function getAttries()
	   {
			$query='select u.name as username,
							p.*,c.name as catname
						from product_attri as p
						left join users as u on p.created_by=u.id
						left join categories as c on c.id=p.catid';
			return $this->_db->loadObjectList($query);
	   }
	   function getAttriItem($id)
	   {
			return $this->_db->loadObject('select * from product_attri where id='.(int)$id);
	   }
	   
		function getCategories()
		
		{
			$query='select * from categories';
			return $this->_db->loadObjectList($query);
			
		}
	   function getProducts($catid)
	   {
			$query='select p.* from products as p 
				left join categories as c on c.id=p.catid
				where p.catid='.$catid;
			return $this->_db->loadObjectList($query);
	   }
	    function getProduct()
	   {
			$query='select p.*,c.name from products as p left join categories as c on c.id=p.mode';
			return $this->_db->loadObjectList($query);
	   }
	   
	   function getProductAll()
	   {
			$query='select p.*,c.name from products as p left join categories as c on c.id=p.mode';
			return $this->_db->loadObjectList($query);
	   }
	    function getProductAll2()
	   {
			$query='select p.*,c.name from products as p left join categories as c on c.id=p.mode';
			return $this->_getList($query,10,$this->getTotal(),'products');
	   }
	   function getArticles($catid)
		{
			$query="select * from article where catid=".(int)$catid." and published=1";
			return $this->_getList($query,5,$this->getTotal($catid),$catid);
		}
		function getTotal()
		{
			$query="select count(id) from products where published=1";
			return $this->_db->loadResult($query);
		}
	  
	   function getAttri($catid)
	   {
		$query='select * from product_attri where catid='.$catid.' order by id';
		return $this->_db->loadObjectList($query);
		
	   }
	   
	    function getAttriValue($attri,$productid)
	   {
			$query='select * from product_attri_value where idattri ='.$attri.' and product_id='.$productid.' order by idattri ';
			$row= $this->_db->loadObject($query);
			return isset($row->value)?$row->value:'';
		
	   }
	   
	   function store1($data,$tablename)
	   {
			$result=$this->store($data,$tablename);
			return $result;
	   }
	   
	   function getItem($id)
	   {
			return $this->_db->loadObject('select p.*,c.name as modename,d.name as catname from products as p left join categories as c on c.id=p.mode left join categories as d on d.id=p.catid where p.id='.$id);
	   }
	   
	   function removeattri($ids)
	   {
		if(is_array($ids))
			$sql='delete from product_attri where id in ('.implode(',',$ids).')';
		else
			$sql='delete from product_attri where id ='.(int)$ids;
		return $this->_db->query($sql);
	   }
	   function getArticle($id)
	   {
		return $this->_db->loadResult('select content from article where id='.(int)$id);
	   }
	   function removeAttriByProductId($productid)
	   {
			$sql='delete from product_attri_value where product_id ='.(int)$productid;
			return $this->_db->query($sql);
	   }
	   function getID()
		{
			return $this->_db->insert_id();
		}
	   function getImages($productid)
		{
			return $this->_db->loadObjectList('select * from product_images where productid='.(int)$productid);
		}
		
		function getImage($productid)
		{
			return $this->_db->loadObjectList('select * from product_images where productid='.(int)$productid.' limit 0,1');
		}
	   
	  
    }
?>
